package com.shop2cn.iapi.subscription.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.annotation.Bean;

import com.shop2cn.iapi.subscription.parameter.*;
import com.shop2cn.iapi.subscription.service.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.funcpackage;
import com.ymttest.database.model.godmch.funcpackageExample;
import com.ymttest.database.model.godmch.subscriptioncharge;
import com.ymttest.database.model.godmch.subscriptionchargeExample;
import com.ymttest.database.sqlwapper.godmch.funcpackageWapper;
import com.ymttest.database.sqlwapper.godmch.subscriptionchargeWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;
/**
*商家付费订购套餐Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ApplyCharge{
	private static ApplyChargeBean applychargeBean;
	private static ApplyChargeCall applychargeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家付费订购套餐Test");
	}

	@Before
	public void caseUp() {
		applychargeBean=new ApplyChargeBean();
		applychargeCall=new ApplyChargeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(applychargeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 验证
	 * @param payUrl 是否返回url
	 * @param charge 是否付费
	 * @param isSetMchChargeConfig 是否设置白名单
	 * @throws InterruptedException
	 * @throws JSONException
	 */
	private void check_verify(boolean payUrl,boolean charge,boolean isSetMchChargeConfig) throws InterruptedException, JSONException {
		
		Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
		JSONObject object=new JSONObject(applychargeCall.getString("data"));
		Logger.verifyEquals(payUrl, object.getString("payUrl").length()>0, "验证payUrl");
		Logger.verifyEquals(charge, object.getBoolean("charge"), "验证object");
		
		//funcpackageWapper
		funcpackageWapper funWapper=new funcpackageWapper();
		funcpackageExample funExample=new funcpackageExample();
		funExample.createCriteria().andFuncPackageIdEqualTo(applychargeBean.getFuncPackageId()).andFuncTypeEqualTo(0);
		funcpackage fun=funWapper.selectByExample(funExample).get(0);
		//subscriptionchargeWapper
		subscriptionchargeWapper chargeMapper=new subscriptionchargeWapper();
		subscriptionchargeExample chargeExample=new subscriptionchargeExample();
		chargeExample.createCriteria().andMchIdEqualTo(applychargeBean.getMchId());
		chargeExample.setOrderByClause(" createTime desc ");
		subscriptioncharge chage=chargeMapper.selectByExample(chargeExample).get(0);
		
		Logger.verifyEquals(fun.getFuncPackageId(), chage.getFuncPackageId(), "验证FuncPackageId");
		Logger.verifyEquals(fun.getFuncType(), chage.getFuncType(), "验证FuncType");
		Logger.verifyEquals(fun.getExpirationDay(), chage.getExpirationDay(), "验证ExpirationDay");
		if(isSetMchChargeConfig==true)
		{
			Logger.verifyEquals(new BigDecimal("0.00"), chage.getChargeAmount(), "验证ChargeAmount");
			Logger.verifyEquals(4, chage.getChargeStatus(), "验证ChargeStatus");
		}
		else
		{
			Logger.verifyEquals(fun.getPackageAmount(), chage.getChargeAmount(), "验证ChargeAmount");
			Logger.verifyEquals(0, chage.getChargeStatus(), "验证ChargeStatus");
		}

	}
	
	
	/**
	 *--设置白名单
	 * GodmchManageCallService.SetTrueMchChargeConfig(mchid, Arrays.asList(0,1,2,3,4),true);
                        Thread.sleep(1000);
      --取消白名单
      GodmchManageCallService.SetTrueMchChargeConfig(mchid, Arrays.asList(0,2,4),false);
                        Thread.sleep(1000);
      -- 查询白名单表 godmch库 ChargeConfig
      
      --申请成功后落库：subscriptionfunc、subscriptioncharge
      //设置报名单，如果funcTypeId=3，subscriptioncharge会新增一条数据
       
       FuncTypeEnum（订购功能）
		0	直播	LIVE
		1	进销存	PSI
		2	独立小程序	WXC
		3	分销员	DIST(distributor简写)
		4	线下支付	OFLP(offlinepay简写)
		5	小程序直播	WX_LIVE
		6	自营商品	SELFP
		
		SubscriptionStatusEnum（订购状态）
		0	未订购	UnSubscription
		1	试用中	Tryout
		2	试用结束	TryoutFinish
		3	订购中	Subscription
		4	订购到期	SubscriptionEnd

	 */
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_001() {
		Logger.start(true,"申请一年直播付费-没有是设置白名单，成功");
		try {
			
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(0);
			applychargeBean.setFuncPackageId(3);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			
			check_verify(true,false,false);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_001_1() {
		Logger.start(true,"申请一年直播付费-设置白名单，成功");
		try {
			//设置报名单，如果funcTypeId=3，subscriptioncharge会新增一条数据
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
            Thread.sleep(1000);
            
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(0);
			applychargeBean.setFuncPackageId(3);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();		
			
			check_verify(true,true,true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_001_2() {
		Logger.start(true,"申请一年直播付费-设置白名单-再取消白名单，成功");
		try {
			
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			 GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
             Thread.sleep(1000);
             
             GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,2,4),false);
             Thread.sleep(1000);
            
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(0);
			applychargeBean.setFuncPackageId(3);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			

			check_verify(true,false,false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_ApplyCharge_002() {
		Logger.start(true,"申请一年直播付费 商户id错误，失败(20200911 商户错误目前开发没有做验证)");
		try {
			applychargeBean.setMchId(1235);
			applychargeBean.setFuncType(0);
			applychargeBean.setFuncPackageId(3);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_003() {
		Logger.start(true,"申请一年直播付费 订购类型错误，失败");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(8);
			applychargeBean.setFuncPackageId(3);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			Logger.verifyEquals("201", applychargeCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_004() {
		Logger.start(true,"申请一年直播付费 套餐类型错误，失败");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(1);
			applychargeBean.setFuncPackageId(5);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			Logger.verifyEquals("201", applychargeCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_005() {
		Logger.start(true,"申请一年线下付款付费，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(4);
			applychargeBean.setFuncPackageId(5);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
			
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_006() {
		Logger.start(true,"存在白名单，申请一年线下付款付费，返回true");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(4);
			applychargeBean.setFuncPackageId(5);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
			Logger.verifyEquals(true, applychargeCall.getJsonObject().getAsJsonObject("data").get("charge").getAsBoolean(),"为true，在白名单内");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_007() {
		Logger.start(true,"不在白名单，申请一年线下付款付费，返回false");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(4);
			applychargeBean.setFuncPackageId(5);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();	
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
			Logger.verifyEquals(false, applychargeCall.getJsonObject().getAsJsonObject("data").get("charge").getAsBoolean(),"为true，在白名单内");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_008() {
		Logger.start(true,"上级开通分销，下级自动开通");
		try {
			
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			Integer DistributorId = GodmchManageCallService.CreateDistributor(mchId);
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			 Thread.sleep(1000);
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(3);
			applychargeBean.setFuncPackageId(6);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();	
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
			Logger.verifyEquals(true, applychargeCall.getJsonObject().getAsJsonObject("data").get("charge").getAsBoolean(),"为true，在白名单内");
		} catch (Exception e) {
			Logger.fail(e);
		} 
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyCharge_009() {
		Logger.start(true,"自营商品套餐，成功");
		try {
			/**
			 * godmch：funcpackage表
			 * FuncPackageId=8    FuncType=6  FunName="自营商品365天"
			 */
			
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			applychargeBean.setMchId(mchId);
			applychargeBean.setFuncType(6);
			applychargeBean.setFuncPackageId(8);
			applychargeBean.setCallbackUrl("http://www.ymatou.com");
			applychargeBean.setClientIp(TestDataManager.getRandomIp());
			applychargeCall.setData(applychargeBean);
			applychargeCall.callService();			
			Logger.verifyEquals("200", applychargeCall.getString("code"), "验证code");
			JSONObject object=new JSONObject(applychargeCall.getString("data"));
			Logger.verifyEquals(true, object.getString("payUrl").length()>0, "验证payUrl");
			Logger.verifyEquals(false, object.getBoolean("charge"), "验证charge");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}